<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>欢迎登录</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        #loginFormDiv {
            border: 1px solid blue;
            margin: 100px auto;
            padding: 50px 20px 20px 50px ;
            width: 600px;
            height: 300px;
            box-shadow: 15px 15px 15px 15px #888888;
        }
    </style>
</head>
<body>
<div id = "loginFormDiv">
<!-- 你的 HTML 代码 -->
<form class="layui-form" action="" lay-filter="example">
    <div class="layui-form-item">
        <label class="layui-form-label">输入框</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码框</label>
        <div class="layui-input-block">
            <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input" lay-verify="title">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">验证码</label>
        <div class="layui-input-inline">
            <input type="text" name="capchar" autocomplete="off" placeholder="请输入验证码" class="layui-input" lay-verify="pass">
        </div>
        <div class="layui-input-inline">
            <img src="/captcha" style="width:85px;height: 38px" id="cap"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否免登录</label>
        <div class="layui-input-block">
            <input type="checkbox" name="autologin" lay-skin="switch" lay-text="ON|OFF">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</div>
<script src="./layui/layui.js"></script>
<script>
    // 加载layui的指定 模块
    layui.use(function(){
        // layui js 代码开始的位置

        var layer = layui.layer
            ,form = layui.form,
            $=layui.$;

        //监听提交
        form.on('submit(demo1)', function(data){
            console.log(data.field);
            // 发送ajax请求
            $.ajax({
                url:'/user/login',
                type:"post",
                data:data.field,
                success:function(ret){
                    console.log(ret);
                    if(ret.code==="-1001"){ // 验证码错误
                        layer.msg(ret.msg);
                    }
                    if(ret.code=="-1"){ // 验证码错误
                        layer.msg(ret.msg);
                    }
                    if(ret.code=="0"){ // 登录成功
                        debugger
                        // layer.msg(ret.msg);
                        window.location.href="/index.html";
                    }
                },
                error:function (ret) {
                    console.log(ret);
                    // 登录失败给出提示信息
                    layer.alert(ret.message, {
                        title: "提示信息",
                    });
                }
            });
            return false; // 取消表单的默认提交行为
        });

        // 注册图片的单击事件
        $("#cap").click(function () {
            path = "/captcha?"+new Date().getTime();
            $(this).attr("src",path);
        });
    });
</script>
</body>
</html>